"""
Case Type   : cm_ctl
Case Name   : 资源池化集群执行cm_ctl pause后,kill -9 主节点cm_agent进程
Create At   : 2024/04/05
Owner       : @kyeleze
Description :
    1.执行cm_ctl pause
    2.查看集群状态
    3.kill -9 主节点cm_agent进程
    4.查看agent进程
    5.恢复集群
Expect      :
    1.pause成功
    2.集群状态normal
    3.成功
    4.agent为新的进程(新的pid)
    5.成功
History     :
"""

import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class CmPauseResumeCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node("PrimaryDbUser")
        self.common = Common()

    def test_cm(self):
        step = '--step1:执行cm_ctl pause expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('pause')
        self.log.info(result)
        self.assertIn(self.constant.cm_pause_success_msg, result,
                      "执行失败:" + step)

        step = '--step2:查询集群状态 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, result,
                      "执行失败:" + step)
        self.assertIn("pausing         : Yes", result,
                      "执行失败:" + step)

        step = '--step3:kill -9 主节点cm_agent进程 expect:成功--'
        self.log.info(step)
        agent_cmd = "ps ux | grep 'cm_agent' | grep -v grep | awk '{{print $2}}'"
        self.log.info(agent_cmd)
        old_agent_pid = self.common.get_sh_result(self.pri_user, agent_cmd)
        self.log.info(old_agent_pid)
        kill_cmd = "ps ux | grep 'cm_agent' | grep -v grep | " \
                   "awk '{{print $2}}' | xargs kill -9"
        self.log.info(kill_cmd)
        result = self.common.get_sh_result(self.pri_user, kill_cmd)
        self.log.info(result)
        self.assertIn('', result, "执行失败:" + step)
        time.sleep(10)

        step = '--step4:查看agent进程 expect:agent为新的进程(新的pid)--'
        self.log.info(step)
        pid_cmd = "ps ux | grep 'cm_agent' | grep -v grep |awk '{{print $2}}'"
        self.log.info(pid_cmd)
        new_agent_pid = self.common.get_sh_result(self.pri_user, pid_cmd)
        self.log.info(result)
        self.assertNotEqual(new_agent_pid,old_agent_pid, "执行失败:" + step)
        time.sleep(10)

        step = '--step5:恢复集群 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('resume')
        self.log.info(result)
        self.assertIn(self.constant.cm_resume_success_msg, result,
                         "执行失败:" + step)
        time.sleep(10)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, result,
                         "执行失败:" + step)

    def tearDown(self):
        step = '----恢复环境----'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("resume")
        self.log.info(result)
        self.assertIn(self.constant.cm_resume_success_msg, result,
                      "执行失败:" + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
